package com.smartum.mpr.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

public abstract class BaseDao<T> {

	protected HibernateTemplate hibernateTemplate;
	protected Logger LOG = Logger.getLogger(getClass());

	public void save(T object) {
		LOG.info(String.format("save %s", getEntityClass()));
		hibernateTemplate.saveOrUpdate(object);
	}

	public void update(T object) {
		LOG.info(String.format("update %s ", getEntityClass()));
		hibernateTemplate.update(object);
	}

	public List<T> getList() {
		LOG.info(String.format("list %s ", getEntityClass()));
		return hibernateTemplate.findByCriteria(DetachedCriteria.forClass(getEntityClass()));
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	protected abstract Class<T> getEntityClass();
}
